export default {
  // 全局方法
  install(Vue, options) {
    Vue.prototype.deepClone = function(obj) {
      if (obj === undefined) {
        return undefined
      }
      // set数据
      try {
        return JSON.parse(JSON.stringify(obj))
      } catch (e) {
        console.error('数据拷贝失败。deepClone：' + e)
      }
    }

    Vue.prototype.re = function(targetarr, contrastarr, key) {
      // 输出对象，校验去处对象
      // 对象数组去重
      try {
        const target = targetarr
        contrastarr.forEach(item => {
          target.forEach((unit, index) => {
            // 去重
            if (item[key] == unit[key]) {
              target.splice(index, 1)
            }
          })
        })
        return target
      } catch (e) {
        console.error('去重失败。re：' + e)
      }
    }
    Vue.prototype.merge = function(json1, json2, key) {
      // 输出对象，校验去处对象
      // 对象数组去重
      const json = json1.concat(json2)
      const newJson = []
      for (const item1 of json) {
        let flag = true
        for (const item2 of newJson) {
          if (item1[key] == item2[key]) {
            flag = false
          }
        }
        if (flag) {
          newJson.push(item1)
        }
      }
      return newJson
    }
    Vue.prototype.restr = function(targetarr, contrastarr) {
      // 一维数组去重
      try {
        const target = targetarr
        contrastarr.forEach(item => {
          target.forEach((unit, index) => {
            // 去重
            if (item == unit) {
              target.splice(index, 1)
            }
          })
        })
        return target
      } catch (e) {
        console.error('去重失败。restr：' + e)
      }
    }
    Vue.prototype.filterSame = function(filterarr, key) {
      // 一维数组去重
      try {
        const target = filterarr
        filterarr.forEach(item => {
          let a = 0
          filterarr.forEach((unit, index) => {
            // 去重
            if (item[key] == unit[key]) {
              a++
              if (a > 1) {
                target.splice(index, 1)
              }
            }
          })
        })
        return target
      } catch (e) {
        console.error('去重失败。restr：' + e)
      }
    }
    Vue.prototype.selectformat = function(value, targetArr, key) {
      // 一维数组去重
      try {
        const target = targetArr.filter(e => e[key] == value)
        return (target && target[0]) || null
      } catch (e) {
        console.error('序列化select失败。selectformat：' + e)
      }
    }
    Vue.prototype.stripscript = function(s) {
      const rs = s.replace(/[^0-9 -]/g, '')
      return rs
    }
    Vue.prototype.validNum = function(s) {
      const rs = s.replace(/[^0-9]/g, '')
      return rs
    }
    Vue.prototype.formatDateTimetoStr = function(date) {
      // 返回当前时间
      if (date == '' || !date) {
        return ''
      }
      const dateobj = new Date(date)
      const y = dateobj.getFullYear()
      let m = dateobj.getMonth() + 1
      m = m < 10 ? '0' + m : m
      let d = dateobj.getDate()
      d = d < 10 ? '0' + d : d
      let h = dateobj.getHours()
      h = h < 10 ? '0' + h : h
      let minute = dateobj.getMinutes()
      minute = minute < 10 ? '0' + minute : minute
      let second = dateobj.getSeconds()
      second = second < 10 ? '0' + second : second
      return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
    }
    Vue.prototype.formatcode = function(key) {
      const dataForm = this.deepClone(this.dataForm)
      dataForm[key] = dataForm[key].replace(/[^A-Za-z0-9_]/g, '')
      this.dataForm = this.deepClone(dataForm)
    }
    Vue.prototype.formatname = function(key) {
      const dataForm = this.deepClone(this.dataForm)
      dataForm[key] = dataForm[key].replace(/[^\u4e00-\u9fa5A-Za-z0-9_]/g, '')
      this.dataForm = this.deepClone(dataForm)
    }
    Vue.prototype.TabReName = function(name, that) {
      // 更改标签名称（更改名称 ，当前vue对象）
      const tempRoute = Object.assign({}, that.$route)
      const title = name
      const route = Object.assign({}, tempRoute, { title })
      route.meta.title = title
      that.$store.dispatch('tagsView/updateVisitedView', route)
    }
    Vue.prototype.closeTab = function(to, that) {
      // 更改标签名称（更改名称 ，当前vue对象）
      const view = that.$route
      that.$store.dispatch('tagsView/delView', view).then(() => {
        that.$router.push({
          name: to
        })
      })
    }
  }
}
